package com.jfs.qy.config.component;


import cn.hutool.core.collection.CollectionUtil;
import com.jfs.qy.entity.response.ChannelInfo;
import com.jfs.qy.entity.response.SearchIpcListInfoResponse;
import com.jfs.qy.entity.response.SearchIpcListInfoResponseData;
import com.jfs.qy.enums.TaskKeyEnum;
import com.jfs.qy.service.SystemIpcListInfoService;
import com.jfs.qy.session.NvrSessionStorage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description 定时查询系统ipc列表详情任务
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/11/5
 */
public class SystemIpcListInfoTask implements Task {
    private final static ReentrantLock Lock = new ReentrantLock(true);
    private final SystemIpcListInfoService systemIpcListInfoService;

    public SystemIpcListInfoTask(SystemIpcListInfoService systemIpcListInfoService) {
        this.systemIpcListInfoService = systemIpcListInfoService;
    }

    @Override
    public String getKey() {
        return TaskKeyEnum.SYSTEM_IPC_LIST_INFO_KEY.getKey();
    }

    @Override
    public void run() {
        try {
            ResponseEntity<SearchIpcListInfoResponse> searchIpcListInfoResponseResponseEntity = this.systemIpcListInfoService.searchIpcListInfo();
            if (Objects.nonNull(searchIpcListInfoResponseResponseEntity) && HttpStatus.OK.equals(searchIpcListInfoResponseResponseEntity.getStatusCode())) {
                SearchIpcListInfoResponseData data = Objects.requireNonNull(searchIpcListInfoResponseResponseEntity.getBody()).getData();
                Lock.lock();
                if (Objects.isNull(data)) {
                    return;
                }
                Map<String, ChannelInfo> channelInfo = data.getChannelInfo();
                if (CollectionUtil.isEmpty(channelInfo)){

                }
                for (String key : channelInfo.keySet()) {
                    //跟新设备信息
                    NvrSessionStorage.CHANNEL_INFO.put(key, channelInfo.get(key));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Lock.unlock();
        }
    }
}
